/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import Bean.Practica;
import Bean.Dispositivo;
import Conexion.Conexion;
import Configuracion.AppConfig;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author kriss
 */
public class DAO_Practica {

    private String SQL = "";
    private PreparedStatement statement;
    ResultSet rs;
    private int id = -1;
    private int bandera = -1;
    private Connection conexion;
    
    public int insertaPractica(Practica prac) {
        
        Conexion con = new Conexion(AppConfig.Servidor, AppConfig.Usuario, AppConfig.Contrasena, AppConfig.BD, AppConfig.Puerto);
      
        conexion = con.getConexion();
        if (conexion != null) {
            try {
                this.SQL = "Insert into practica (numero_practica,descripcion,desarrollo,software,configuracion_script,version) values (?,?,?,?,?,0);";
               
                 this.statement = conexion.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
      
                this.statement.setInt(1, prac.getNumero_practica());
                this.statement.setString(2, prac.getDescripcion());
                this.statement.setString(3, prac.getDesarrollo());
                this.statement.setString(4, prac.getSoftware());
                this.statement.setString(5, prac.getConfiguracion_script());
//                this.statement.setString(6, prac.getCelular());
//                this.statement.setString(7, prof.getCorreo());
                this.bandera = this.statement.executeUpdate();
                System.out.println(""+statement.toString());
                if (bandera == 1) {
                    rs = statement.getGeneratedKeys();
                    while (rs.next()) { 
                        id = rs.getInt(1);
                    }
                }
                if (this.statement != null) { 
                    this.statement.close();              }
            } catch (Exception e) { 
                System.out.println("Practica insertaPractica: " + e.getMessage());
                con.cerrarConexion(); 
            } finally {
                con.cerrarConexion(); 
            }
        }

        return this.id;
    }
    public ArrayList<Practica> getListaPracticas() {
        Conexion con = new Conexion(AppConfig.Servidor, AppConfig.Usuario, AppConfig.Contrasena, AppConfig.BD, AppConfig.Puerto);
        ArrayList<Practica> lista = new ArrayList<Practica>();

        try {

            SQL = "SELECT * FROM practica;";
            System.out.println(SQL);

            statement = con.getConexion().prepareStatement(SQL);

            ResultSet result = statement.executeQuery();

            while (result.next()) {

                Practica infDispositivo = new Practica();
                infDispositivo.setId(Integer.parseInt(result.getObject("id").toString()));
                infDispositivo.setNumero_practica(Integer.parseInt(result.getObject("numero_practica").toString()));
                infDispositivo.setImagen(result.getObject("imagen").toString());
                System.out.println(result.getObject("imagen").toString());
                infDispositivo.setDescripcion(result.getObject("descripcion").toString());
                infDispositivo.setDesarrollo(result.getObject("desarrollo").toString());
                System.out.println(result.getObject("desarrollo").toString());
//                infDispositivo.setSoftware(result.getObject("software").toString());
//                System.out.println("sin errores1");
//                infDispositivo.setConfiguracion_script(result.getObject("configuracion_script").toString());
//                System.out.println("sin errores2");
//                infDispositivo.setVersion(Integer.parseInt(result.getObject("version").toString()));
//                System.out.println("sin errores3");
                lista.add(infDispositivo);

                System.out.println("------------------------------------------->"+lista.size());
            }

            if (statement != null) {
                statement.close();
            }
        } catch (Exception e) {
            System.out.println("Practica practicaID: " + e.toString());
            con.cerrarConexion();
        } finally {
            con.cerrarConexion();
        }
//        System.out.println(lista.get(0).getDesarrollo());
        System.out.println(lista.get(0));
        return lista;
    }
}
